<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('orders')) {
            //总订单表
            Schema::create('orders', function (Blueprint $table) {
                $table->increments('id')->comment('自增id');
                $table->integer('user_id')->default(0)->comment('用户id');
                $table->integer('activity_id')->default(0)->comment('活动id');
                $table->string('order_num')->unique()->comment('订单编号');
                $table->decimal('m_amount')->default(0)->comment('订单原总额');
                $table->decimal('s_amount')->default(0)->comment('订单实际总额');
                $table->string('type')->nullable()->comment('common:常规订单;integral:积分订单;group:团购订单;bargain:砍价订单;sign_up报名订单;');
                $table->integer('group_type')->default(0)->comment('拼团类型 1：单独购买 2：发起拼团 3：参加拼团');
                $table->integer('group_refund')->default(0)->comment('是否退款 0：未退款 1：已退款 2：退款失败');
                $table->integer('group_id')->default(0)->comment('拼团id');
                $table->integer('invoice_id')->default(0)->comment('发票信息id');
                $table->string('express_num',100)->nullable()->comment('配送单号');
                $table->string('express')->nullable()->comment('快递公司');
                $table->decimal('express_fee')->default(0)->comment('快递费用');
                $table->timestamp('express_time')->nullable()->comment('发货时间');
                $table->integer('address_id')->default(0)->comment('订单的配送地址id');
                $table->integer('is_pay')->default(0)->comment('是否已支付，0：未支付，1：已支付');
                $table->timestamp('pay_time')->nullable()->comment('支付时间');
                $table->string('pay_type')->nullable()->comment('支付方式，有weixin、alipay、bank');
                $table->string('note')->nullable()->comment('备注');
                $table->integer('status')->default(0)->comment('订单状态订单状态，0：未确认，1：确认订单,待付款，2：商家已确认，待提货，3：已提货，完成，4:关闭交易，5：逾期取货');
                $table->softDeletes();
                $table->timestamps();
            });
        }


        if (!Schema::hasTable('order_item')) {
            //子订单表
            Schema::create('order_item', function (Blueprint $table) {
                $table->increments('id')->comment('自增id');
                $table->integer('goods_id')->default(0)->comment('商品id');
                $table->integer('goods_spu_id')->default(0)->comment('商品id');
                $table->integer('order_id')->comment('订单id');
                $table->string('item_num')->nullable()->comment('子订单编号');
                $table->decimal('m_price')->default(0)->comment('商品原单价');
                $table->string('s_price')->default(0)->comment('商品打折后单价');
                $table->integer('num')->default(0)->comment('购买数量');
                $table->string('type')->nullable()->comment('订单类型：common:常规订单;integral:积分订单;group:团购订单;bargain:砍价订单;sign_up报名订单');
                $table->integer('goods_snapshot_id')->default(0)->comment('快照id');
                $table->integer('status')->default(0)->comment('子订单状态 0：未评价 1：已评价');
                $table->softDeletes();
                $table->timestamps();
            });
        }


        if (!Schema::hasTable('order_address')) {
            //订单发货地址表
            Schema::create('order_address', function(Blueprint $table) {
                $table->increments('id')->comment('地址标识');
                $table->integer('user_id')->comment('用户标识');
                $table->string('consignee',20)->nullable()->comment('收件人');
                $table->string('province',30)->nullabel()->comment('省份');
                $table->string('city',30)->nullable()->comment('城市');
                $table->string('area',30)->nullable()->comment('地区');
                $table->string('address')->nullable()->comment('详细地址');
                $table->string('postcode',10)->nullable()->comment('邮政编码');
                $table->string('phone',20)->nullable()->comment('手机号');
                $table->string('telephone',20)->nullable()->comment('固定电话号码');
                $table->softDeletes();
                $table->timestamps();
            });
        }


        if (!Schema::hasTable('goods_snapshot')) {
            //商品快照表
            Schema::create('goods_snapshot', function(Blueprint $table) {
                $table->increments('id')->comment('自增id');
                $table->integer('goods_id')->default(0)->comment('商品id');
                $table->string('ssn',20)->nullable()->comment('商品货号');
                $table->string('name',50)->nullable()->comment('商品名称');
                $table->string('goods_spu_name')->nullable()->comment('商品规格名称');
                $table->decimal('m_price')->default(0)->comment('市场价格');
                $table->decimal('s_price')->default(0)->comment('销售价格');
                $table->integer('thumb_id')->default(0)->comment('商品缩略图');
                $table->text('info')->nullable()->comment('商品信息描述');
                $table->integer('sale_count')->default(0)->comment('销售数量');
                $table->string('type')->nullable()->comment('common:常规商品;integral:积分商品,group:团购商品');
                $table->integer('exchange_integral')->default(0)->comment('兑换积分');
                $table->integer('integral')->default(0)->comment('购买商品获得的积分');
                $table->softDeletes();
                $table->timestamps();
            });
        }

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
